package com.example.helllo;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class MyList3Activity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{
    private static final String TAG = "MyList3Activity";
    ListView listView;
    RateAdapter adapter;
    ArrayList<HashMap<String,String>>listItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list2);
        listView = findViewById(R.id.mylist2);
        ProgressBar progressBar=findViewById(R.id.progressBar);
        progressBar.setVisibility(View.GONE);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        //获取网络数据
        Handler handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==11){
                    //进行消息处理
                    //ArrayList<String> list2=(ArrayList<String>)msg.obj;

                    //ListAdapter adapter=new ArrayAdapter<String>(FirstListActivity.this, android.R.layout.simple_list_item_1,list2);
                    //setListAdapter(adapter);//设置数据适配器，把当前界面用adapter绑定
                    ArrayList<Rate> listItems = (ArrayList<Rate>) msg.obj;
//                    SimpleAdapter listItemAdapter=new SimpleAdapter(MyList3Activity.this,
//                            listItems,
//                            R.layout.list_item,
//                            new String[]{"Country","Rate"},
//                            new int[]{R.id.itemTitle,R.id.itemDetail}
//                    );
                    adapter=new RateAdapter(MyList3Activity.this,R.layout.list_item,(ArrayList<Rate>) msg.obj);
                    listView.setAdapter(adapter);
                    //listView.setAdapter(listItemAdapter);
                    //setListAdapter(listItemAdapter);
                    listView.setVisibility(View.VISIBLE);

                }
                super.handleMessage(msg);
            }
        };
        RateTask task=new RateTask();
        task.setHandler(handler);
        Thread t=new Thread(task);
        t.start();
    }

    @Override
    //监听
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //获得当前行对应的对象
        Object itemAtPosition=listView.getItemAtPosition(position);
        Rate item=(Rate) itemAtPosition;
        //点击行获取数据
        String countryStr=item.getCname();
        String rateStr=item.getCval();
        Log.i(TAG,"onItemClick:countryStr"+countryStr);
        Log.i(TAG,"onItemClick:rateStr"+rateStr);
        //启动子线程
        Intent config = new Intent(this, CountRateActivity.class);
        config.putExtra("count_country", countryStr);
        config.putExtra("count_rate", rateStr);
        Log.i(TAG, "openCountRate:count_country=" + countryStr);
        Log.i(TAG, "openCountRate:count_rate=" + rateStr);
        //Log.i(TAG,"openConfig:dollarRate:",dollarRate);
        startActivityForResult(config, 8);
        //删除数据项
        //adapter.remove(item);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: 长按事件 position="+position);
        Rate item=(Rate) listView.getItemAtPosition(position);
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
            builder.setTitle("提示")
                    .setMessage("请确认是否删除当前数据")
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.i(TAG, "onClick: 对话框事件处理");
                           //删除数据项
                            adapter.remove(item);
                        }
                    }).setNegativeButton("取消",null);
            builder.create().show();

        return true;
    }
}